package com.chuanyang.driverh.mainhome.adapter;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Paint;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.mainhome.MoneyFollowWaybilListActivity;
import com.chuanyang.driverh.mainhome.OrderDetailRceiveActivity;
import com.chuanyang.driverh.response2.ResultMoneyFollow;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.StringUtil;

import java.util.List;

import static com.chuanyang.driverh.R.id.ll_see_assign;


public class MoneyFollowAdapter extends BaseListAdapter<ResultMoneyFollow.OutData.MiddleData.InnderData> {//首页竞价大厅的adapter
    private Activity activity;

    public MoneyFollowAdapter(Activity activity) {
        super(activity);//此处不传入集合，则默认集合长度为0，在setdataresult（）方法中，添加数据并刷新
        this.activity = activity;
    }

    @Override
    public View getZkView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = getInflater().inflate(R.layout.home_item_money_follow, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.line_left = convertView.findViewById(R.id.line_left);
            viewHolder.tv_has_money = (TextView) convertView.findViewById(R.id.tv_has_money);
            viewHolder.tv_order_no = (TextView) convertView.findViewById(R.id.tv_order_no);
            viewHolder.tv_order_time = (TextView) convertView.findViewById(R.id.tv_order_time);
            viewHolder.tv_city_start = (TextView) convertView.findViewById(R.id.tv_city_start);
            viewHolder.tv_city_end = (TextView) convertView.findViewById(R.id.tv_city_end);
            viewHolder.tv_goods_name = (TextView) convertView.findViewById(R.id.tv_goods_name);
//            viewHolder.tv_weight = (TextView) convertView.findViewById(R.id.tv_weight);
            viewHolder.tv_price = (TextView) convertView.findViewById(R.id.tv_price);
            viewHolder.tv_acture_weight_sum = (TextView) convertView.findViewById(R.id.tv_acture_weight_sum);
            viewHolder.tv_gua_weight_num = (TextView) convertView.findViewById(R.id.tv_gua_weight_num);
            viewHolder.tv_money = (TextView) convertView.findViewById(R.id.tv_money);
            viewHolder.tv_order_type = (TextView) convertView.findViewById(R.id.tv_order_type);
            viewHolder.tv_see_assign = (TextView) convertView.findViewById(R.id.tv_see_assign);
            viewHolder.ll_div = (LinearLayout) convertView.findViewById(R.id.ll_div);
            viewHolder.ll_see_assign = (LinearLayout) convertView.findViewById(ll_see_assign);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();

        }
        final ResultMoneyFollow.OutData.MiddleData.InnderData data = getItem(position);
//        viewHolder.tv_time.setText(data.getExpiredTime());
        if (data.getDefrayStatus() == 0) {// 	0 未收款 2 已收款
            viewHolder.line_left.setBackgroundResource(R.color.text_assign_car_gray);
            viewHolder.tv_has_money.setTextColor(activity.getResources().getColor(R.color.text_assign_car_gray));
            viewHolder.tv_has_money.setText("未收款");
        } else {
            viewHolder.line_left.setBackgroundResource(R.color.text_get_order_orange);
            viewHolder.tv_has_money.setTextColor(activity.getResources().getColor(R.color.text_get_order_orange));
            viewHolder.tv_has_money.setText("已收款");
        }
        viewHolder.tv_order_no.setText(data.getOrderNo());
        viewHolder.tv_order_time.setText(data.getOrderTime());
        viewHolder.tv_city_start.setText(StringUtil.getSimpleMunicipality(data.getBeginCity()));
        viewHolder.tv_city_end.setText(StringUtil.getSimpleMunicipality(data.getEndCity()));
//        setGoodName(viewHolder.tv_goods_name, data.getWaybills());//货品名称
        viewHolder.tv_goods_name.setText(data.getGoodsNames());

        viewHolder.tv_acture_weight_sum.setText(StringUtil.getDouble(data.getSumDetailsWeight() + "")+"t/"+data.getSumDetailsNum()+"件");
        viewHolder.tv_gua_weight_num.setText("挂牌"+StringUtil.getDouble(data.getOriginalWeight() + "")+"t/"+data.getOriginalNum()+"件");
        viewHolder.tv_gua_weight_num.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
//        viewHolder.tv_weight.setText(StringUtil.getDouble(data.getOriginalWeight()+""));

        String curency="¥";

        if (data.getCurrency()==1){
            curency="¥";
        }else {
            curency="$";
        }
        viewHolder.tv_price.setText(StringUtil.getDouble(data.getPrice()+"")+curency + "/t");

        viewHolder.tv_money.setText(StringUtil.getTwoNum(data.getSumPrice()+"" )+ curency);


        if (data.getFleetId()!=0) {
            if (data.getOrderType() == 1) {//1竞价
                viewHolder.tv_order_type.setBackgroundResource(R.mipmap.order_bid_t);
            } else if (data.getOrderType() == 2) {//匹配
                viewHolder.tv_order_type.setBackgroundResource(R.mipmap.order_match_t);
            } else {//专线
                viewHolder.tv_order_type.setBackgroundResource(R.mipmap.order_special_t);
            }
        } else {
            if (data.getOrderType() == 1) {//1竞价
                viewHolder.tv_order_type.setBackgroundResource(R.mipmap.order_bid_p);
            } else if (data.getOrderType() == 2) {//匹配
                viewHolder.tv_order_type.setBackgroundResource(R.mipmap.order_match_p);
            } else {//专线
                viewHolder.tv_order_type.setBackgroundResource(R.mipmap.order_special_p);
            }
        }
        viewHolder.ll_div.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!StringUtil.isBlank(SharedPrefUtil.getFleetId())) {
                    Intent intent = new Intent(activity, OrderDetailRceiveActivity.class);
                    intent.putExtra("data", data.getOrderId() + "");
                    intent.putExtra("type", "0");
                    if (data.getFleetId() != 0){
                        intent.putExtra("type", "11");
                    }
                    activity.startActivity(intent);

                } else {
//                    Intent intent=new Intent(activity, WaybilDetailActivity.class);
//                    intent.putExtra("data",data.getOrderId()+"");
//                    intent.putExtra("type","0");
//                    activity.startActivity(intent);

                }

            }
        });

        if (StringUtil.isBlank(SharedPrefUtil.getFleetId())) {
            viewHolder.ll_see_assign.setVisibility(View.GONE);
        } else {
            viewHolder.ll_see_assign.setVisibility(View.VISIBLE);
        }


        if (data.getFleetId() != 0) {//车队接单
            viewHolder.ll_see_assign.setVisibility(View.VISIBLE);
            viewHolder.tv_see_assign.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(activity, MoneyFollowWaybilListActivity.class);
                    intent.putExtra("currency", data.getCurrency()+"");
                    intent.putExtra("data", data.getOrderId() + "");
                    activity.startActivity(intent);
                }
            });

        } else {
            viewHolder.ll_see_assign.setVisibility(View.GONE);
        }
        return convertView;
    }


    private void setGoodName(TextView view, List<ResultMoneyFollow.OutData.MiddleData.InnderData.WaybillsBean> waybils) {
        StringBuilder builder = new StringBuilder();
        if (waybils == null) {
            view.setText("");
            return;
        }
        for (ResultMoneyFollow.OutData.MiddleData.InnderData.WaybillsBean info : waybils) {
            if (info != null)
                builder.append(info.getGoodsName() + ",");
        }
        String temp = builder.toString();
        if (temp.length() > 0) {
            String result = temp.substring(0, temp.length() - 1);
            view.setText(result);
        }
    }

    class ViewHolder {
        View line_left;
        TextView tv_has_money;
        TextView tv_order_no;
        TextView tv_order_time;
        TextView tv_city_start;
        TextView tv_city_end;
        TextView tv_goods_name;
        //        TextView tv_weight;
        TextView tv_price;
        TextView tv_acture_weight_sum;
        TextView tv_gua_weight_num;
        TextView tv_money;
        TextView tv_order_type;
        TextView tv_see_assign;
        LinearLayout ll_div;
        LinearLayout ll_see_assign;
    }
}
